﻿using STF.Blog.Dtos;
using STF.Blog.IRepository;
using STF.Blog.IService;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace STF.Blog.Service
{
    public class ReleaseFormService : IReleaseFormService
    {
        private IReleaseFormManager _rel;
        public ReleaseFormService(IReleaseFormManager rel)
        {
            _rel = rel;
        }
        public async Task<int> AddRfAsync(string title, string details)
        {
            return await _rel.AddAsync(new Model.ReleaseForm 
            {
                Title = title,
                Details = details
            });
        }

        public async Task<int> DeleteRfsAsync(int id)
        {
            var data = await _rel.GetById(id);
            if (data == null)
                return -1;
            if (data.DeleteId != 1)
                return -2;
            return await _rel.DeleteAsync(data);
        }

        public async Task<List<ReleaseFormDto>> GetAllRfAsync(int deleteId = 1)
        {
            var data = await _rel.GetChooseAsync(rf => rf.DeleteId == deleteId);
            return data == null ? new List<ReleaseFormDto>() : data.Select(rf => new ReleaseFormDto 
            {
                Id = rf.Id,
                Title = rf.Title,
                Details = rf.Details,
                CreateTime = rf.CreateTime,
                UpdateTime = rf.UpdateTime
            }).ToList();
        }

        public async Task<ReleaseFormDto> GetByIdAsync(int id)
        {
            var data = await _rel.GetById(id);
            return data == null ? new ReleaseFormDto() : new ReleaseFormDto 
            {
                Id = data.Id,
                Title = data.Title,
                Details = data.Details,
                CreateTime = data.CreateTime,
                UpdateTime = data.UpdateTime,
            };
        }

        public async Task<List<ReleaseFormDto>> GetByNameAsync(string title, int deleteId = 1)
        {
            var data = await _rel.GetChooseAsync(rf => rf.Title.Contains(title) && rf.DeleteId == deleteId);
            return data == null ? new List<ReleaseFormDto>() : data.Select(rf => new ReleaseFormDto 
            {
                Id = rf.Id,
                Title = rf.Title,
                Details = rf.Details,
                CreateTime = rf.CreateTime,
                UpdateTime = rf.UpdateTime,
            }).ToList();
        }

        public async Task<int> PutTrashAsync(int id)
        {
            var data = await _rel.GetById(id);
            if(data == null || data.DeleteId == 0)
                return -1;
            data.DeleteId = 0;
            return await _rel.UpdateAsync(data);
        }

        public async Task<int> RestoreAsync(int id)
        {
            var data = await _rel.GetById(id);
            if (data == null || data.DeleteId == 1)
                return -1;
            data.DeleteId = 1;
            return await _rel.UpdateAsync(data);
        }

        public async Task<int> UpdateRfAsync(int id, string title, string details)
        {
            var data = await _rel.GetById(id);
            if (data == null)
                return -1;
            data.Title = title;
            data.Details = details;
            data.UpdateTime = DateTime.Now;
            return await _rel.UpdateAsync(data);
        }
    }
}
